#!/usr/bin/perl
# Copyright (C) 1999-2003 by Salvador E. Tropea (SET),
# see copyrigh file for details
#
# Creates a distribution tarball.
#
require "../miscperl.pl";

GetVersion('../');

$r=cat('../include/tv.h');
if ($r!~/$Version/)
  {
   die "Error! inconsistent version in tv.h\n";
  }

# Adjust Debian version
$r=cat('../debian/changelog');
if ($r!~/$Version/)
  {
   chdir('../../debian');
   @ENV{'DEBEMAIL'}='salvador@inti.gov.ar';
   @ENV{'DEBFULLNAME'}='Salvador E. Tropea (SET)';
   system("dch -v$Version"."-0.1 \"New version automatically modified by version update scripts\"");
   chdir('../makes/linux');
  }

$doBinary=0;
$useBzip2=0;
$useVersioInSourceDir=0;
foreach $i (@ARGV)
  {
   if ($i eq '--binary')
     {
      $doBinary=1;
     }
   elsif ($i eq '--use-bzip2')
     {
      $useBzip2=1;
     }
   elsif ($i eq '--dir-version')
     {
      $useVersioInSourceDir=1;
     }
   else
     {
      print "Unknown command line option: $i\n";
      print "Usage: compres.pl [--binary] [--use-bzip2] [--dir-version]\n\n";
     }
  }

# BC++ 5.5 Makefile
chdir('..');
`perl confignt.pl`;
chdir('linux');
# Patch the version number in the readme.txt
print "Processing readme file\n";
$r=cat('../readme.in');
$r=~s/\@version\@/$Version/g;
replace('../readme.txt',$r);

# Patch Debian control file
print "Processing debian/control file\n";
$r=cat('../debian/control.in');
$r=~s/\@version\@/$Version/g;
replace('../debian/control',$r);

$ext=$useBzip2 ? '.bz2' : '.gz';
$compresor=$useBzip2 ? 'bzip2 -9c' : 'gzip -9c';
$dir='tvision'           unless $useVersioInSourceDir;
$dir='librhtv-'.$Version if     $useVersioInSourceDir; # Mainly for the RPM

# Generate the tar files
$bindist="rhtvision-$Version.bin.i386.elf.static.linux.tar$ext";
$binddist="rhtvision-$Version.bin.i386.elf.dynamic.linux.tar$ext";
$srcdist="rhtvision-$Version.src.tar$ext";
chdir('../..');
unlink($srcdist,$bindist,$binddist);

`mv tvision $dir` if $useVersioInSourceDir;

open(FIL,$dir.'/lista');
@files=<FIL>;
close(FIL);
$r='';
foreach $i (@files)
  {
   chop $i;
   if ((substr($i,0,1) eq '-') or (substr($i,0,1) eq '+'))
     {
      $i=substr($i,1);
     }
   $r.=$dir.'/'.$i.' ';
  }
# Add example makefiles
@files=glob($dir.'/examples/*/*.?mk');
$r.=' '.join(' ',@files);
@files=glob($dir.'/examples/*/*/*.?mk');
$r.=' '.join(' ',@files);
@files=glob($dir.'/examples/*/*.mkf');
$r.=' '.join(' ',@files);
@files=glob($dir.'/examples/*/*/*.mkf');
$r.=' '.join(' ',@files);
# Sort the list
@files=split(/ /,$r);
@files=sort(@files);
$r=join(' ',@files);

print "$r\n" if $useVersioInSourceDir;
system("tar -cvf - $r | $compresor > $srcdist");
system("tar -cvf - --exclude=*CVS `cat $dir/linux/inclubin.txt` | $compresor > $bindist")  if $doBinary;
system("tar -cvf - --exclude=*CVS `cat $dir/linux/includbi.txt` | $compresor > $binddist") if $doBinary;
`mv $dir tvision` if $useVersioInSourceDir;

# Generate the lsm distribution file (Linux Software Map entry).
$lsm=
"Begin3
Title:          Turbo Vision (Robert Hoehne port)
Version:        $Version
Entered-date:   !3
Description:    Turbo Vision port to Linux. C++ Text UI. Not 100% Borland compatible.
Keywords:       c++ cui tui framework interface tv tvision
Author:         robert.hoehne\@gmx.net.de (Robert Hoehne)
                salvador\@inti.gov.ar (Salvador E. Tropea)
Maintained-by:  salvador\@inti.gov.ar (Salvador E. Tropea)
Primary-site:   www.geocities.com /SiliconValley/Vista/6552/tvision.html
                !1
                !2
                !4
Original-site:  ftp.borland.com /pub/borlandcpp/devsupport/archive/turbovision
                232k tv.zip
Platforms:      Linux, GNU g++, gpm, ncurses
Copying-policy: GPL
End";

$r= int((-s $srcdist)/1024+0.5)."k $srcdist";
$lsm =~ s/\!1/$r/g;
$r='';
$r= int((-s $bindist)/1024+0.5)."k $bindist" if $doBinary;
$lsm =~ s/\!2/$r/g;
$r= int((-s $binddist)/1024+0.5)."k $binddist" if $doBinary;
$lsm =~ s/\!4/$r/g;
@t=localtime(time());
@mes=('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
$r=$t[3].$mes[$t[4]].$t[5];
$r='0'.$r if length($r)<7;
$lsm =~ s/\!3/$r/g;
replace('rhtvision.lsm',$lsm);
replace('rhtvision.txt',$lsm);


